﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;

namespace Ulriksen.Umbraco.OpenId
{
    public static class MemberExtensions
    {
        public static void AttachOpenid(this umbraco.cms.businesslogic.member.Member member, string claimedIdentifier)
        {

            Application.SqlHelper.ExecuteNonQuery("INSERT INTO nuMemberOpenid (OpenId,nodeId) values (@OpenId,@NodeId)",
               Application.SqlHelper.CreateParameter("@OpenId", claimedIdentifier),
               Application.SqlHelper.CreateParameter("@NodeId", member.Id));
        }
        public static void RemoveOpenId(this umbraco.cms.businesslogic.member.Member member, string openIdIdentifier)
        {
            Application.SqlHelper.ExecuteNonQuery("delete from nuMemberOpenid where OpenId = @OpenId",
                Application.SqlHelper.CreateParameter("@OpenId", openIdIdentifier));

        }

        public static List<string> GetOpenIds(this umbraco.cms.businesslogic.member.Member member)
        {
            List<string> openIds = new List<string>();

            IRecordsReader openIdReader = Application.SqlHelper.ExecuteReader("select OpenId from nuMemberOpenId where nodeId = @nodeId",
                Application.SqlHelper.CreateParameter("@nodeId", member.Id));

            while (openIdReader.Read())
            {
                openIds.Add(openIdReader.GetString("OpenId"));
            }
            openIdReader.Close();
            return openIds;
        }

    }
}
